package com.shop.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.shop.entity.User;
import com.shop.shop.mapper.UserMapper;
import com.shop.shop.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shop.utile.PasswordEncryp;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 登录检查
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("ur_name", user.getUrName());
        User one = getOne(wrapper);
        if (PasswordEncryp.IfPassowrd(user.getUrPassword(), one.getUrPassword())) {
            return one;
        }
        return null;
    }

    /**
     * 密码修改
     * @param oldPassword
     * @param urPassword
     * @param id
     * @return
     */
    @Override
    public int updatePaaword(String oldPassword, String urPassword, Integer id) {
        User user = baseMapper.selectById(id);
        if (user != null) {
            if (PasswordEncryp.IfPassowrd(oldPassword, user.getUrPassword())) {
                //旧密码正确   修改密码并且加密
                //密码加密
                String paawordEncryp = PasswordEncryp.getPaawordEncryp(urPassword);
                //修改数据
                user.setUrPassword(paawordEncryp);
                boolean b = updateById(user);
                return b ? 1 : 0;
            }

        }
        return 0;
    }


}
